package org.zyx.item.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.zyx.item.entity.SpecGroup;
import org.zyx.item.entity.SpecParam;
import org.zyx.item.service.SpecificationService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: SwordMaple
 * @Date: 2020/9/6  20:18
 * @Description: 负责管理规格参数组及参数
 * @Version: 1.0
 */
@RestController
@RequestMapping("/spec")
public class SpecificationHandler {

    @Resource
    private SpecificationService specificationService;

    /**
     * 通过分类id,查找对应的规格参数组
     * @param cid 分组id(category)
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> findGroupsByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> SpecGroups = specificationService.findGroupsByCid(cid);
        if(CollectionUtils.isEmpty(SpecGroups)){    //为空则返回未找到
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(SpecGroups);
    }

    /**
     * 通过根据条件查找参数
     * @param gid
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> findParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching
            ){

        List<SpecParam> specParams = specificationService.findParams(gid,cid,generic,searching);
        if(CollectionUtils.isEmpty(specParams)){    //为空则返回未找到
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }

    /**
     * 添加参数分组
     * @param specGroup json对象
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity addSpecGroup(@RequestBody SpecGroup specGroup){
        if(!specificationService.addSpecGroup(specGroup)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 添加参数分组
     * @param specGroup json对象
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity updSpecGroup(@RequestBody SpecGroup specGroup){
        if(!specificationService.updSpecGroup(specGroup)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id删除参数分组
     * @param id
     * @return
     */
    @DeleteMapping("/group/{id}")
    public ResponseEntity deleteSpecGroup(@PathVariable("id") Long id){
        if(!specificationService.deleteSpecGroupById(id)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 添加参数
     * @param specParam
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity addParam(@RequestBody SpecParam specParam){
        if(!specificationService.addParam(specParam)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 更新参数
     * @param specParam
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity updParam(@RequestBody SpecParam specParam){
        if(!specificationService.updParam(specParam)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 移除参数
     * @param id
     * @return
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity deleteParam(@PathVariable("id") Long id){
        if(!specificationService.deleteSpecParamById(id)){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok().build();
    }


    /**
     * 通过cid查找分类组级参数
     * @param cid
     * @return
     */
    @GetMapping("/group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> findGroupsWithParam(@PathVariable("cid") Long cid){
        List<SpecGroup> specGroupList = specificationService.findGroupsWithParam(cid);
        if (CollectionUtils.isEmpty(specGroupList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroupList);
    }







}
